package com.example.sign

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.example.base.entity.SignEntity
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.UiState
import com.example.sign.databinding.ActivitySignRecordBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/sign/SignRecordActivity")
@AndroidEntryPoint
class SignRecordActivity : BaseActivity<ActivitySignRecordBinding,SignIntent,SignViewModel>() {
    val signMultiAdapter by lazy { SignMultiAdapter() }
    override fun getLayout(): Int  = R.layout.activity_sign_record

    override fun getVM(): Class<SignViewModel>  = SignViewModel::class.java

    override fun initView() {
        binding.rv.apply {
            adapter = signMultiAdapter
            layoutManager = LinearLayoutManager(this@SignRecordActivity)
        }
        viewModel.sendIntent(SignIntent.signRecord(SPUtils.getInstance().getInt("uid").toString()))

        binding.head.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.text = "打卡记录"
            headAdd.isVisible = false
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            viewModel.uiState.collect{
                when(it){

                    is UiState.onSuccess<*> -> {
                        val signEntity = it.data as SignEntity
                        signMultiAdapter.apply {
                            data.clear()
                            signEntity.forEachIndexed { index, signEntityItem ->
                                signEntityItem.type = index%2
                            }
                            data.addAll(signEntity.sortedByDescending { it.id })
                            notifyDataSetChanged()
                        }
                    }
                    else->{

                    }
                }
            }
        }
    }

}